package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.redp.bizimpl.enums.PageOptFlagEnum;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpBMchntBaseInfo;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpBMchntLevelInfo;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpBMchntBaseInfoService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpBMchntLevelInfoService;
import cc.rengu.redp.bizimpl.upmp.vo.OperationResult;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商户等级
 * </p>
 *
 * @author 刘玉亮.LL
 * @since 2021-06-22
 */
@Api(tags = "商户管理:商户等级管理")
@Slf4j
@RestController
@RequestMapping("/admin/mcmp/bMchntLevelInfo")
public class BMchntLevelInfoController extends BaseController {

    private long oprMenuId = 20000022;
    private String oprMenuName = "商户管理:商户等级管理";
    private long chkMenuId = 20000022;
    private String chkMenuName = "商户管理:商户等级管理";
    private String webNextUrl = "bizimpl/upmp/mchntLevel/MchntLevelChk";


    @Autowired
    private IUpmpBMchntLevelInfoService mchntLevelInfoService;

    @Autowired
    IUpmpBMchntBaseInfoService mchntBaseInfoService;

    @GetMapping(value = "/list")
//    @RequiresPermissions("areaInfo:view")
    public Map<String, Object> list(QueryRequest request, UpmpBMchntLevelInfo entity) {
        return getDataTable(this.mchntLevelInfoService.findByPage(request, entity));
    }

    @Log("编辑查询")
    @GetMapping(value = "/edit")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "主键", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "optFlag", value = "主操作类型:新增|修改|删除|审核等", dataType = "String")
    })
    public Map<String, Object> getEdit(@NotBlank(message = "{required}") String
                                               optFlag, @NotBlank(message = "{required}") String instId, String id) throws RedpException {
        try {
            return mchntLevelInfoService.getEdit(optFlag, instId, id);
        } catch (RedpException e) {
            String message = "查询失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改")
    @PostMapping("/edit")
//    @RequiresPermissions("areaInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "optFlag", value = "主操作类型:新增|修改|删除|审核等", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required = true, dataType = "String")
    })
    public OperationResult update(@Valid UpmpBMchntLevelInfo entity) throws RedpException {
        String optFlag = entity.getOptFlag() == null ? "" : entity.getOptFlag();
        String menuId;
        String cancelReason = entity.getCancelReason();
        if (PageOptFlagEnum.OPT_FLAG_CHECK.getType().equals(optFlag)) {
            menuId = String.valueOf(chkMenuId);
//            entity = mchntLevelInfoService.getById(entity.getId());
        } else if (PageOptFlagEnum.OPT_FLAG_CANCEL.getType().equals(optFlag)) {
            menuId = String.valueOf(oprMenuId);
            entity = mchntLevelInfoService.getById(entity.getId()); } else {
            menuId = String.valueOf(oprMenuId);
        }
        entity.setOptFlag(optFlag);
        entity.setCancelReason(cancelReason);
        return mchntLevelInfoService.saveData(menuId, oprMenuName, entity, webNextUrl);
    }


    @GetMapping(value = "/mchntLevelSelect2List")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "q", value = "参数", required = true, dataType = "String")
    })
    public Map mchntLevelSelect2List(String q, QueryRequest request) {
        return mchntLevelInfoService.mchntLevelSelect2List(q, request);
    }

    /**
     * 审批
     *
     * @param entity
     * @return
     */
    //@RequiresPermissions("商户管理:商户等级管理")
    @PostMapping(value = "/editPass")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "optFlag", value = "主操作类型:新增|修改|删除|审核等", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "主键", required = true, dataType = "String")
    })
    public OperationResult editpass(@Valid UpmpBMchntLevelInfo entity) throws RedpException {
        String auditRefuseReason = entity.getAuditRefuseReason();
        String cancelReason = entity.getCancelReason();
        String stopReason = entity.getStopReason();
        entity = mchntLevelInfoService.getById(entity.getId());
        entity.setAuditRefuseReason(auditRefuseReason);
        entity.setCancelReason(cancelReason);
        entity.setStopReason(stopReason);

        String menuId;
        if (PageOptFlagEnum.OPT_FLAG_CHECK.getType().equals(entity.getOptFlag())) {
            menuId = String.valueOf(chkMenuId);
        } else {
            menuId = String.valueOf(oprMenuId);
        }

        return mchntLevelInfoService.chkData(menuId, oprMenuName, entity, webNextUrl);
    }

    @Log("删除")
    //@RequiresPermissions("商户管理:商户等级管理")
    @DeleteMapping("/{ids}")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "id主键，逗号隔开", required = true, dataType = "String")
    public OperationResult delete(@NotBlank(message = "{required}") @PathVariable String ids) {
        String[] idsArr = ids.split(StringPool.COMMA);
        java.util.Map<String, Object> result = com.google.common.collect.Maps.newHashMap();
        String[] idGroup = ids.split(StringPool.COMMA);
        for (String s : idGroup) {
            UpmpBMchntLevelInfo entity = mchntLevelInfoService.getById(s);
            String status = entity.getMchntLevelStatus();
            if (!BusinessContainer.status_03.equals(status)) {
                return OperationResult.buildFailureResult("非新增审批拒绝的集团商户，不能进行删除", result);
            }
        }
        mchntLevelInfoService.delete(idGroup);
        return OperationResult.buildSuccessResult("删除成功", result);
    }

    /**
     * 注销商户等级时校验是否有商户在使用该商户等级
     *
     * @return
     */

    //@RequiresPermissions("商户管理:商户等级管理")
    @PostMapping(value = "/cancelQuery")
    @ApiImplicitParam(paramType = "path", name = "mchntLevel", value = "商户等级", required = true, dataType = "String")
    public OperationResult cancelQuery(@NotBlank(message = "{required}") String mchntLevel) throws RedpException {
        List<UpmpBMchntBaseInfo> list = mchntBaseInfoService.getBMchntBaseInfoByMchntLevel(mchntLevel);
        if (list != null && list.size() > 0) {
            return OperationResult.buildSuccessResult("N");
        } else {
            return OperationResult.buildSuccessResult("Y");
        }
    }
}
